package com.ideal.manage.esms.repository.system;


import com.ideal.manage.esms.bean.sys.Menu;
import com.ideal.manage.esms.bean.sys.Role;
import com.ideal.manage.esms.bean.sys.RoleMenu;
import com.ideal.manage.esms.repository.framework.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface RoleMenuRepository extends BaseRepository<RoleMenu, String> {

    @Query("select rm.menu from RoleMenu rm where rm.menu.delFlag = 0 and rm.role = ?1 and rm.menu.type = ?2  order by rm.menu.sort")
    List<Menu> getAllMenu(Role role,Long menuType);

    @Query("select rm.menu.code from RoleMenu rm where rm.menu.delFlag = 0 and rm.role = ?1 and rm.menu.type = 2 ")
    List<String> getAllButtonCodeByRole(Role role);

    @Query("select a from RoleMenu a where a.role.id = ?1 and a.menu.id = ?2")
    RoleMenu findByRoleAndMenu(String roleId, String menuId);

    @Modifying
    @Query("delete from RoleMenu a where a.role.id = ?1")
    void deleteByRoleId(String roleId);

    @Query("select rm.menu from RoleMenu rm where rm.menu.delFlag = 0 and rm.role = ?1 and rm.menu.type = ?2 and rm.menu.parentMenu is null  order by rm.menu")
    List<Menu> getAllMainMenu(Role role,Long menuType);
}
